package com.gzstv.dlnasdk.util;

import com.gzstv.dlnasdk.upnp.Device;
import com.gzstv.dlnasdk.upnp.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;
import java.net.URL;

public class XmlParser {

    public static void parseDeviceDescription(Device device, String xmlContent) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // 关键：开启命名空间支持
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(xmlContent)));
            doc.getDocumentElement().normalize();

            XPath xpath = XPathFactory.newInstance().newXPath();

            // 使用 XPath 提取设备基本信息
            String friendlyName = xpath.evaluate("//*[local-name()='device']/*[local-name()='friendlyName']/text()", doc);
            String udn = xpath.evaluate("//*[local-name()='device']/*[local-name()='UDN']/text()", doc);
            String presentationURL = xpath.evaluate("//*[local-name()='device']/*[local-name()='presentationURL']/text()", doc);

            if (friendlyName != null && !friendlyName.isEmpty()) {
                device.setFriendlyName(friendlyName);
            }
            if (presentationURL != null && !presentationURL.isEmpty()) {
                try {
                    device.setPresentationUrl(new URL(presentationURL));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            // 使用 XPath 提取服务列表
            NodeList serviceNodes = (NodeList) xpath.evaluate("//*[local-name()='serviceList']/*[local-name()='service']", doc, XPathConstants.NODESET);

            URL baseURL = new URL(device.getLocation());

            for (int i = 0; i < serviceNodes.getLength(); i++) {
                Node serviceNode = serviceNodes.item(i);
                if (serviceNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element serviceElement = (Element) serviceNode;

                    String serviceType = xpath.evaluate("*[local-name()='serviceType']/text()", serviceElement);
                    String serviceId = xpath.evaluate("*[local-name()='serviceId']/text()", serviceElement);
                    String controlURLPath = xpath.evaluate("*[local-name()='controlURL']/text()", serviceElement);
                    String eventSubURLPath = xpath.evaluate("*[local-name()='eventSubURL']/text()", serviceElement);

                    try {
                        URL controlURL = new URL(baseURL, controlURLPath);
                        URL eventSubURL = new URL(baseURL, eventSubURLPath);

                        Service service = new Service(serviceType, serviceId, controlURL, eventSubURL);
                        device.addService(service);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
